package com.example.xwattention.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.xwcommon.entity.Attention;
import com.example.xwcommon.entity.User;
import com.example.xwcommon.service.AttentionService;
import com.example.xwcommon.service.UserService;
import com.example.xwcommon.vo.Response;
import io.seata.core.context.RootContext;
import io.seata.core.exception.TransactionException;
import io.seata.spring.annotation.GlobalTransactional;
import io.seata.tm.api.GlobalTransactionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AttentionApi {
    @Autowired
    @Qualifier("attentionService")
    private AttentionService attentionService;

    @Autowired
    @Qualifier("com.example.xwcommon.service.UserService")
    private UserService userService;

    @RequestMapping("/api/attention/addAttention")//添加关注
    @GlobalTransactional
    public ResponseEntity<Response<Integer>> addAttention(String attentionName, String userName) throws TransactionException {
        try{
            Response<Integer> attentionResponse = attentionService.addAttention(attentionName, userName);
            System.out.println("addAttention//："+attentionResponse.getCode());
            if(attentionResponse.getCode() == 200){
                System.out.println("addAttention"+attentionResponse.getCode());
                Response<User> userResponse = userService.updateAttentionCount(attentionName,1);
                if(userResponse.getCode() == 200){
                    return ResponseEntity.ok(attentionResponse);
                }
                else{
                    GlobalTransactionContext.reload(RootContext.getXID()).rollback();
                    return ResponseEntity.status(500).body(new Response<>(500,"更新关注数失败",null));
                }
            }
            else{
                GlobalTransactionContext.reload(RootContext.getXID()).rollback();
                //return ResponseEntity.status(500).body(new Response<>(500,"关注失败",null));
            }
        } catch (TransactionException e) {
            GlobalTransactionContext.reload(RootContext.getXID()).rollback();
            throw new RuntimeException(e);
            //return ResponseEntity.status(500).body(new Response<>(500,"关注失败",null));
        }
        return ResponseEntity.status(500).body(new Response<>(500,"关注失败",null));
    }
    @RequestMapping("/api/attention/getAttentionById")//根据id获取关注
    public ResponseEntity<Response<Attention>> getAttentionById(Integer attentionId) {
        Response<Attention> response = attentionService.getAttentionById(attentionId);
        if(response.getCode() != 200){
            return ResponseEntity.status(500).body(response);
        }
        return ResponseEntity.ok(response);
    }
    @RequestMapping("/api/attention/getAttentionListByLoginName")//获取关注列表
    public ResponseEntity<Response<Page<Attention>>> getAttentionListByLoginName(Integer pageNum, Integer pageSize, String loginName) {
        Response<Page<Attention>> response = attentionService.getAttentionListByLoginName(pageNum, pageSize, loginName);
        if(response.getCode() != 200){
            return ResponseEntity.status(500).body(response);
        }
        return ResponseEntity.ok(response);
    }
    @RequestMapping("/api/attention/removeAttention")//取消关注
    @GlobalTransactional
    public ResponseEntity<Response<Integer>> removeAttention(Integer attentionId) throws TransactionException {
        try{
            Attention attention = attentionService.getAttentionById(attentionId).getData();
            Response<Integer> attentionResponse = attentionService.removeAttention(attentionId);
            if(attentionResponse.getCode() == 200){
                //Attention attention = attentionService.getAttentionById(attentionId).getData();
                Response<User> userResponse = userService.updateAttentionCount(attention.getAttentionName(),-1);
                if(userResponse.getCode() == 200){
                    return ResponseEntity.ok(attentionResponse);
                }
                else{
                    GlobalTransactionContext.reload(RootContext.getXID()).rollback();
                    return ResponseEntity.status(500).body(new Response<>());
                }
            }
            else{
                GlobalTransactionContext.reload(RootContext.getXID()).rollback();
                //return ResponseEntity.status(500).body(new Response<>(500,"取消关注失败",null));
            }
        }catch (TransactionException e){
            GlobalTransactionContext.reload(RootContext.getXID()).rollback();
            throw new RuntimeException(e);
        }
        return ResponseEntity.status(500).body(new Response<>(500,"取消关注失败",null));
    }
}
